package fr.vincent7894.feedroid;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import fr.vincent7894.feedroid.feed.context.impl.RssFeedContext;
import fr.vincent7894.feedroid.feed.pojo.Feed;
import fr.vincent7894.feedroid.feed.pojo.FeedAlias;
import fr.vincent7894.feedroid.feed.task.AbstractProgressDialogFeedTask;
import fr.vincent7894.feedroid.feed.task.impl.RssFeedTask;
import fr.vincent7894.feedroid.fragment.FeedAliasesFragment;
import fr.vincent7894.feedroid.listener.FeedAliasClickListener;
import fr.vincent7894.feedroid.utils.Logger;

public class MainActivity extends FragmentActivity {

	private AbstractProgressDialogFeedTask dialogFeedTask;

	private FeedAliasesFragment feedAliasesFragment;
	
	private FragmentManager fragmentManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectAll().penaltyLog().build());
		
		this.fragmentManager = super.getSupportFragmentManager();

		feedAliasesFragment = (FeedAliasesFragment) super
				.getSupportFragmentManager().findFragmentById(
						R.id.aliases_fragment);
		feedAliasesFragment.setFeedAliasClickListener(new DefaultFeedAliasClickListener());
		
		dialogFeedTask = new RssFeedTask(new ProgressDialog(this));
	}

	@Override
	protected void onResume() {
		super.onResume();

		FeedAlias alias = new FeedAlias();
		alias.setUri("http://www.oracle.com/us/corporate/press/rss/rss-pr.xml");
		RssFeedContext ctx = new RssFeedContext();
		ctx.setEnableCache(false);
		alias.setFeedContext(ctx);

		this.dialogFeedTask.getMediator().getWidget()
				.setOnCancelListener(new DialogCancelListener());
		this.dialogFeedTask.getMediator().getWidget()
				.setOnDismissListener(new DialogDismissListener());
		this.dialogFeedTask.execute(alias);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/**
	 * On cancel listener
	 * 
	 * @author vincent
	 * 
	 */
	class DialogCancelListener implements OnCancelListener {

		@Override
		public void onCancel(DialogInterface dialog) {
			Logger.info("Last error is : " + dialogFeedTask.getLastError());
		}
	}

	/**
	 * On dismiss listener
	 * 
	 * @author vincent
	 * 
	 */
	class DialogDismissListener implements OnDismissListener {

		@Override
		public void onDismiss(DialogInterface dialog) {
			if (!dialogFeedTask.isCancelled()) {
				try {
					for (Feed feed : dialogFeedTask.get()) {
						Logger.info("Feed title is : %s", feed.getTitle());
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	class DefaultFeedAliasClickListener implements FeedAliasClickListener {

		@Override
		public void onFeedAliasClick(FeedAlias feedAlias) {
			FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
			
			fragmentTransaction.commit();
		}
	}
}
